#include <sys_config.h>
#include <types.h>
#include <basic_types.h>
#include <mediatypes.h>
#include <api/libosd/osd_lib.h>

#include "menu_api.h"
#include "com_api.h"

#ifdef _INVW_JUICE
#include "graphics/inviewpfm_graphics.hpp"

extern void GraphicsLayerShowTo(void);
#endif
UINT16 display_strs[MAX_DISP_STR_ITEMS][MAX_DISP_STR_LEN];
UINT16 len_display_str[MAX_LEN_DISP_STR];

#define MAX_WINS_ON	20
POBJECT_HEAD g_menuStack[MAX_WINS_ON];
static UINT8 curOnWinNum = 0;

#ifdef DVR_PVR_SUPPORT
POBJECT_HEAD g_menuStack_back[MAX_WINS_ON];
static UINT8 curOnWinNum_back = 0;

void menu_stack_backup(void)
{
	UINT8 i;
	curOnWinNum_back = curOnWinNum;
	for(i=0; i<curOnWinNum; i++)
	{
		g_menuStack_back[i] = g_menuStack[i];
	}
}

void menu_stack_recover(void)
{
	UINT8 i;
	curOnWinNum = curOnWinNum_back;
	for(i=0; i<curOnWinNum_back; i++)
	{
		g_menuStack[i] = g_menuStack_back[i];
	}
	curOnWinNum_back = 0;
}
#endif

void menu_stack_push(POBJECT_HEAD w)
{
	if(curOnWinNum>=MAX_WINS_ON)
		return;
	g_menuStack[curOnWinNum++] = w;
}

void menu_stack_push_ext(POBJECT_HEAD w, INT8 shift)
{
	UINT8	i;
	
	if(curOnWinNum>=MAX_WINS_ON)
		return;

	if(shift >= 0)
		menu_stack_push(w);
	else
	{
		if((0 - shift) > curOnWinNum)
			return;
		for(i = curOnWinNum + shift; i < curOnWinNum ; i++)
		{
			g_menuStack[i + 1] = g_menuStack[i];
		}
		g_menuStack[(UINT8)((INT8)curOnWinNum + shift)] = w;
		curOnWinNum ++;
	}
}

void menu_stack_pop(void)
{
	if(curOnWinNum>0)
		curOnWinNum --;
#ifdef _INVW_JUICE
	// 20120614 - Mark: Also needs to check if we are in record playing or not
	// If we are in record playing, then no need to set the Inview OSD again
   
    // we appear to be calling this even when we a transitioning from Inview UI to Ali, that doesn't seem right.
    //	if ((curOnWinNum == 0) && (api_pvr_is_playing() != TRUE) && (api_pvr_is_rec_only_or_play_rec() != TRUE)) GraphicsLayerShowTo();

	libc_printf("+++++++++%s %d curonwinnum %d+++++++++++\n", __FUNCTION__,__LINE__,curOnWinNum);
#endif
}

void menu_stack_pop_all(void)
{
	curOnWinNum = 0;
}


POBJECT_HEAD menu_stack_get_top(void)
{
	if(curOnWinNum==0)
		return NULL;

	return g_menuStack[curOnWinNum - 1];
}


POBJECT_HEAD menu_stack_get(int offset)
{
    if(offset>=0)
    {
        if(offset<curOnWinNum)
            return g_menuStack[offset];
        else
            return NULL;
    }
    else //offset<0
    {
        if(curOnWinNum + offset > 0)
            return g_menuStack[curOnWinNum -1 + offset];
        else
            return NULL;
    }
        
}

PRESULT menu_enter_root(POBJECT_HEAD p_handle, UINT32	para)
{
	PRESULT         	proc_ret = PROC_PASS;
	ControlMsg_t msg;
	static POBJECT_HEAD	p_prev_handle;
	
	if((p_handle!=NULL) && (p_handle != menu_stack_get_top()))
	{
	//	if(NULL == menu_stack_get_top())
	//		api_osd_mode_change(OSD_WINDOW);//Guoping Modify, for window use osd-0 individually
		//ap_mute_hide(p_handle);
		if(NULL == menu_stack_get_top())
		{
			//ap_time_display_hide(p_handle);
		}
		proc_ret = OSD_ObjOpen(p_handle, para);
		if(PROC_LEAVE != proc_ret)
		{
			menu_stack_push(p_handle);
		}
	}	

	return proc_ret;
}

void display_strs_init(UINT8 start, UINT8 num)
{
	UINT8 i = 0;

	for (i = start; i < (start + num); i++)
	{
		MEMSET(display_strs[i], 0, sizeof(display_strs[i]));
	}
}

UINT8 menu_stack_get_cnt()
{
	return curOnWinNum;
}

